package com.cskaoyan.demo7.controller;

import com.cskaoyan.demo7.bean.req.CartCheckedVo;
import com.cskaoyan.demo7.bean.resp.BaseRespVo;
import com.cskaoyan.demo7.bean.resp.CartIndexVo;
import com.cskaoyan.demo7.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/23 19:36
 */

@RestController
@RequestMapping("/wx/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping  ("index")
    public BaseRespVo index() {
        Integer userId = 1;
        CartIndexVo cartIndexVo = cartService.index(userId);
        return BaseRespVo.ok(cartIndexVo);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedVo cartCheckedVo){
        // {"productIds":[152],"isChecked":0}
        Integer userId = 1;
        if (userId == null) {
            return BaseRespVo.ok(0);
        }
        // CartIndexVo cartIndexVo = cartService.checked(userId, map);
        CartIndexVo cartIndexVo = cartService.checked(userId, cartCheckedVo);
        return BaseRespVo.ok(cartIndexVo);
    }

    @PostMapping("add")
    public BaseRespVo add(@RequestBody Map map) {
        // {"goodsId":1181062,"number":4,"productId":319}
        Integer userId = 1;
        if (userId == null) {
            return BaseRespVo.ok(0);
        }
        Object data =cartService.add(userId, map);
        if("库存不足".equals(data)){
            return BaseRespVo.err(711, "库存不足");
        }
        if("商品不存在".equals(data)){
            return BaseRespVo.err(711, "商品不存在");
        }
        return BaseRespVo.ok(data);
    }

    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody Map map) {
        // {"goodsId":1181062,"number":4,"productId":319}
        Integer userId = 1;
        if (userId == null) {
            return BaseRespVo.ok(0);
        }
        Object data =cartService.fastadd(userId, map);
        if("库存不足".equals(data)){
            return BaseRespVo.err(711, "库存不足");
        }
        if("商品不存在".equals(data)){
            return BaseRespVo.err(711, "商品不存在");
        }
        return BaseRespVo.ok(data);
    }

}
